package com.boot.mybatis.rest;

import com.boot.mybatis.entity.Account;
import com.boot.mybatis.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    AccountService accountService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Account> getAccounts() {
        return accountService.findAccountList();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Account getAccountById(@PathVariable("id") int id) {
        return accountService.findAccount(id);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,
                                @RequestParam(value = "money", required = true) double money) {
        int t= accountService.update(name,money,id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable(value = "id")int id) {
        int t= accountService.delete(id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postAccount(@RequestParam(value = "name") String name,
                              @RequestParam(value = "money") double money) {

        int t= accountService.add(name,money);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }


    // 为了方便测试事务，使用get请求
    @GetMapping(value = "/pay1/{id1}/{id2}/{money}")
    public String pay1(@PathVariable("id1") int id1,@PathVariable("id2") int id2,   @PathVariable(value = "money") double money) {
        String msg = accountService.pay1(id1, id2, money);
        return msg;
    }


    // 为了方便测试事务，使用get请求
    @GetMapping(value = "/pay2/{id1}/{id2}/{money}")
    public String pay2(@PathVariable("id1") int id1,@PathVariable("id2") int id2,   @PathVariable(value = "money") double money) {
        String msg = accountService.pay2(id1, id2, money);
        return msg;
    }



    // 为了方便测试事务，使用get请求
    @GetMapping(value = "/pay3/{id1}/{id2}/{money}")
    public String pay3(@PathVariable("id1") int id1,@PathVariable("id2") int id2,   @PathVariable(value = "money") double money) {
        try {
            String msg = accountService.pay3(id1, id2, money);
            return msg;
        }catch (Exception ex) {
            return ex.getMessage();
        }
    }


    // 为了方便测试事务，使用get请求
    @GetMapping(value = "/error1/{id1}/{id2}/{money}")
    public String payError1(@PathVariable("id1") int id1,@PathVariable("id2") int id2,   @PathVariable(value = "money") double money) {
        try {
            String msg = accountService.payError1(id1, id2, money);
            return msg;
        }catch (Exception ex) {
            return ex.getMessage();
        }
    }

    // 为了方便测试事务，使用get请求
    @GetMapping(value = "/error2/{id1}/{id2}/{money}")
    public String payError2(@PathVariable("id1") int id1,@PathVariable("id2") int id2,   @PathVariable(value = "money") double money) {
        try {
            String msg = accountService.payError2(id1, id2, money);
            return msg;
        }catch ( Exception ex) {
            return  ex.getMessage();
        }
    }

}
